<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of product
 *
 * @author sonUIT
 */
class product extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library("loadpage");
    }

    /* load all product */

    public function index() {
        
    }

    /* load products of catalog */

    public function catalog($id) {
        $this->load->model('mproduct');
        $data = array();
        $pro = new Mproduct();
        $data['bang'] = $pro->GetAllProducts();
        $this->load->view('header_view');
        $this->load->view('top-level1_view');
        $this->load->view('slideshow_view');
        $this->load->view('main_start_view');
        $this->load->view('allproduct_view');
        $this->load->view('main_end_view');
        $this->load->view('footer_view');
    }

    public function Detail() {
        if (isset($_GET['matp'])) {
            $matp = $_GET['matp'];
            $data = array();
            $this->load->model("mproduct");
            $sach = new Mproduct();
            $sach->Get($matp);
            if ($sach->Get($matp)) {
                $data['sach'] = $sach;
                $this->load->model("mtacgia");
                $tacgia = new Mtacgia();
                $tacgia->GetByMatg($sach->matg);
                $data['tacgia'] = $tacgia;
                $this->load->model("mnhaxuatban");
                $nxb = new Mnhaxuatban();
                $nxb->GetByManxb($sach->manxb);
                $data['nxb'] = $nxb;
                $data['title'] = "Chi tiết sản phẩm";
                $lp = new loadpage();
                $this->load->model("mtheloai");
                $theloai = new Mtheloai();
                $data['theloai'] = array();
                $data['theloai'] = $theloai->getListObject();
                $data['sachcungtg'] = array();
                $data['sachcungtg'] = $sach->GetSachByMaTg($sach->matg);
                $lp->load(array('product_detail_view'), $data);
                /*
                  $this->load->view('header_view', $data);
                  $this->load->view('top-level1_view');
                  $this->load->view('main_start_view');
                  $this->load->view("product_detail_view");
                  //$this->load->view('allproduct_view');
                  $this->load->view('main_end_view');
                  $this->load->view('footer_view'); */
            }
        }
        else
            echo"trang k ton tai";
    }

    public function GetAll() {
        $this->load->model('mproduct');
        $mproducts = new Mproduct();
        $data = array();
        $data['products'] = $mproducts->GetAllProducts();
        //  $data['tacgias'] = array();
        $this->load->model("mtacgia");
        $tacgia = new Mtacgia();
        $data['tacgias'] = array(array());
        foreach ($data['products'] as $row) {
            $tacgia->GetByMatg($row["MATG"]);

            array_push($data['tacgias'], $tacgia);
        }
        // var_dump($data['tacgias']);
        $this->load->view('products_view', $data);
    }
    public function Get()
    {
         $matp = $_GET['matp'];       
        $a= '<img src="data:image/jpeg;base64,' . $this->Img($matp) . '"/>';
        echo $a;
      
      
    }
    public function Img($matp){
        $this->load->model("mtacpham");
        //$matp = $_GET['matp'];
        $tacpham = new Mtacpham();
        $tacpham->getObject("matp='" . $matp . "'");
        return  base64_encode($tacpham->get_hinhanh());
    }

    public function GetImage($matp) {

        $this->load->model("mtacpham");
        //$matp = $_GET['matp'];
        $tacpham = new Mtacpham();
        $tacpham->getObject("matp='" . $matp . "'");
     //   if (isset($_GET['download'])) {
              
         //   echo strlen($tacpham->get_hinhanh());
            $t1='Content-Disposition: download; filename="' . $matp . '.jpg"';
            $t2="Content-Length:" . strlen($tacpham->get_hinhanh());
            $t3=('Content-type: application/force-download ');
            $t4=("Content-transfer-encoding: binary"); 
           // header('Content-Disposition: download; filename="' . $matp . '.jpg"');
//            header("Content-Length:" . strlen($tacpham->get_hinhanh()));
//            header('Content-type: "image/jpg" ');
//            header("Content-transfer-encoding: binary");          
           // echo base64_encode ($tacpham->get_hinhanh());
            //echo $t1.$t2.$t3.$t4;
            ///echo $tacpham->get_hinhanh();
            header($t1);
            header($t2);
            header($t3);
            header($t4);
            ob_clean();
              echo  ($tacpham->get_hinhanh());
      //  }
//        else
//        { 
//
//              ob_clean();
//              //flush();
//             // header('Content-type: "image/jpg" ');
//           // echo  ($tacpham->get_hinhanh());
//           echo '<img src="data:image/jpeg;base64,' . base64_encode($tacpham->get_hinhanh()) . '"/>';
//        }
    }

}

?>
